package com.example.finaltest.login;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.finaltest.R;
import com.example.finaltest.utils.BaseFragment;
import com.example.finaltest.utils.URLUtils;
import com.google.gson.Gson;

import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册页面
 */
public class RegFragment extends BaseFragment {
    EditText telEt, nicknameEt, pwdEt, repwdEt,questionEt,answerEt;
    Button ensureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_reg, container, false);
        initView(view);
        return view;

    }

    private void initView(View view) {
        telEt = view.findViewById(R.id.frag_reg_et_tel);
        nicknameEt = view.findViewById(R.id.frag_reg_et_name);
        pwdEt = view.findViewById(R.id.frag_reg_et_pwd);
        repwdEt = view.findViewById(R.id.frag_reg_et_repwd);
        questionEt = view.findViewById(R.id.reg_question);
        answerEt = view.findViewById(R.id.reg_answer);
        ensureBtn = view.findViewById(R.id.frag_reg_btn);
        ensureBtn.setOnClickListener(listener);
    }
//
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //获取输入内容
            String tel = telEt.getText().toString();
            String nickname = nicknameEt.getText().toString();
            String pwd = pwdEt.getText().toString();
            String repwd = repwdEt.getText().toString();
            String  question = questionEt.getText().toString();
            String answer = answerEt.getText().toString();
            boolean flag = judgeInput(tel, nickname, pwd, repwd);
            if (flag) { //可以执行网络请求
                Map<String, String> map = new HashMap<>();
                map.put("phone", tel);
                map.put("password", pwd);
                map.put("name", "");
                map.put("address", "");
                map.put("nickname",nickname);
                map.put("question",question);
                map.put("answer",answer);
                postNetwork(URLUtils.register_url, map);
            }
        }
    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:   //注册成功
                Toast.makeText(getContext(), "注册成功！", Toast.LENGTH_SHORT).show();

                break;
            case 201:   //电话号码已经被注册
                Toast.makeText(getContext(), "手机号码已经被注册，请直接登录！", Toast.LENGTH_SHORT).show();
                break;
            default:   //其他，注册失败
                Toast.makeText(getContext(), "注册失败！", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    /* 判断输入的内容是否符合要求*/
    private boolean judgeInput(String tel, String nickname, String pwd, String repwd) {
//        判断输入内容为空，则不能注册
        if (TextUtils.isEmpty(tel) || TextUtils.isEmpty(nickname)
                || TextUtils.isEmpty(pwd) || TextUtils.isEmpty(repwd)) {
            Toast.makeText(getContext(), "输入内容不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!pwd.equals(repwd)) {
            Toast.makeText(getContext(), "两次输入密码不相同", Toast.LENGTH_SHORT).show();
            return false;
        }
//        判断输入的电话号码是否符合要求
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        boolean matches = tel.matches(regex);
        if (!matches) {
            Toast.makeText(getContext(), "输入手机号码不合法！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
